# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql


class SunPipeline:

    file = None
    
    # TODO 学会数据库后来实现一下往数据库存储
    
    def process_item(self, item, spider):
        # 有两个方法提供item 需要判别时那个方法提供的item
        # 一个item主页面 一个item详情页
        if item.__class__.__name__ == 'SunItem':
            print(item['novel_id'], item['novel_author'], item['novel_name'])
            return item
        else:
            # print(item['novel_id'], '\n', item['novel_detail'])
            pass
        
        # return item
    
    
class MysqlPipeline:
    
    database = None
    
    def open_spider(self, spider):
        self.database = pymysql.connect(
            host='localhost', user='root', password="root",
            database='pysql', port=3306, charset ='utf8'
        )
        print('数据库连接成功！')
        
    def process_item(self, item, spider):
        if item.__class__.__name__ == 'SunItem':
            cursor = self.database.cursor()
            sql = """
                insert into t_novel(id, name, author) values(%s, %s, %s);
            """
            cursor.execute(sql, [int(item['novel_id']), item['novel_author'], item['novel_name']])
            self.database.commit()
            cursor.close()
        
    def close_spider(self, spider):
        self.database.close()
        print('数据库关闭！')
    
    
